// MyWork.cpp: implementation of the CMyWork class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "lrc2srt.h"
#include "MyWork.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMyWork::CMyWork()
{
	printf("CMyWork::CMyWork   v0.0.28...\n");
}

CMyWork::~CMyWork()
{

}

void CMyWork::pl2Do(char *src, char *dst)
{
	if(NULL!=src && NULL != dst)
	{
		prOpenSrcFile(src);

		printf("----------------------------------------\n");
		prRenewTime1Set();
		prShowSet();
	}
}

void CMyWork::prOpenSrcFile(char *f)
{ 
	FILE* fpBase = NULL; 

	if((fpBase=fopen(f,"r"))==NULL)
	{ 
		printf("fopen fail!!!\n");
		return ;
	}
	 
	int		n = 0;
	char	szTime[25] = {0};
	int		nTime = 0;
	bool	bTime = false;
	char	szLrc[256] = {0};
	bool	bLrc = false;
	int		nLrc = 0;
	while(1){
		char c=fgetc(fpBase);
		if(feof(fpBase)) break; 
		switch(c)
		{
		case '[':
			{
				bTime = true;
				n++;

				if(nLrc>0)
				{
				//	printf(" %s \n",szLrc);
					strcpy(m_lrcA.m_szLrc,szLrc);

					m_lrcSet.push_back(m_lrcA);
				}

				nLrc = 0;
				szLrc[nLrc] = 0;
			//	printf("n = %d \n",n);
				break;
			}
		case ']':
			{
			//	printf("[%s]\n",szTime);
				strcpy(m_lrcA.szTime0,szTime);
				bTime = false;
				nTime = 0;
				szTime[nTime]=0;
 
				break;
			}
		default:
			{
				if(bTime)
				{
					szTime[nTime] = c;
					nTime++;
					szTime[nTime] = 0;
				}
				else
				{
					szLrc[nLrc] = c;
					nLrc++;
					szLrc[nLrc] = 0;
				}
				break;
			}	
		}
	}
	fclose(fpBase);	
	fpBase = NULL;

}

void CMyWork::prShowSet()
{	
	std::vector<CMyLrc>::iterator it;
	int i = 0;
	for ( it = m_lrcSet.begin(); it != m_lrcSet.end(); ++ it)
	{
		i++;
		printf("%d\n",i);
	//	(*it).plShowStrMsg();
		(*it).plShowSrtMsg();
	} 
}

void CMyWork::prRenewTime1Set()
{
	std::vector<CMyLrc>::iterator it,itLast;
	int i = 0;
	char sz1[256]={"ab"};
	for ( it = m_lrcSet.begin(); it != m_lrcSet.end(); ++ it)
	{
		i++; 		
		if(i>1){
			strcpy((*itLast).szTime1,(*it).szTime0);
		}

		itLast = it;
	} 

}



void CMyWork::prShowSrtSet()
{
	std::vector<CMyLrc>::iterator it;
	int i = 0;
	for ( it = m_lrcSet.begin(); it != m_lrcSet.end(); ++ it)
	{
		i++;
		(*it).plShowSrtMsg();
	} 

}
